using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ItemSpace;

public class ItemsPanel : BaseUI
{
    
    public GameObject cell;
    
    public int cellNumber;
    PlayerInfo playerInfo;
    protected new void Awake()
    {
        base.Awake();
        SetCellNumber();
    }
    private new void Start()
    {
        base.Start();
        playerInfo = player.playerInfo;
        player.itemsPanel = this;
    }
    private void SetCellNumber()
    {
        cellNumber = transform.childCount;
    }

    public void SetAllItems()
    {
        for (int i = 0; i < playerInfo.items.Count; i++)
        {
            GameObject itemCell=null;
            cellUI cellObject=null;
            if(i>=transform.childCount)
            {
                itemCell = Instantiate(cell);
                itemCell.transform.SetParent(transform);
                itemCell.transform.localScale = Vector3.one;
                cellObject = itemCell.GetComponent<cellUI>();
                cellObject.itemsPanel = this;
                cellNumber++;
            }
            else{
                itemCell=transform.GetChild(i).gameObject;
                cellObject=itemCell.GetComponent<cellUI>();
            }
            cellObject.SetItem(playerInfo.items[i]);
            cellObject.SetImage();
            
        }
    }
    public void SetItem(int index)
    {
        GameObject itemCell = null;
        cellUI cellObject = null;
        if (index >= transform.childCount)
        {
            itemCell = Instantiate(cell);
            itemCell.transform.SetParent(transform);
            itemCell.transform.localScale = Vector3.one;
            cellObject = itemCell.GetComponent<cellUI>();
            cellObject.itemsPanel = this;
            cellNumber++;
        }
        else
        {
            itemCell = transform.GetChild(index).gameObject;
            cellObject = itemCell.GetComponent<cellUI>();
            
        }
        
        cellObject.SetItem(playerInfo.items[index]);
        cellObject.SetImage();


    }
    public void DeleteItem(int index)
    {
        GameObject gm =transform.GetChild(index).gameObject;
        Destroy(gm);
        cellNumber--;
    }
}
